<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Dcat\Admin\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Tab;
use App\Models\Media;
use App\Models\Device;
use App\Models\Company;
use Encore\Admin\Controllers\HasResourceActions;
use Log;
use Faker\Factory;
use Dcat\Admin\Widgets\Dropdown;
use Dcat\Admin\Widgets\Card;
use App\Admin\Renderable\DeviceTable;
use Illuminate\Http\Request;


class MediaController extends Controller
{

    public function index(Content $content)
    {
        return $content
            ->header('媒体列表')
            ->description()
            ->body($this->grid());
    }
    public function audit(Content $content){
        // print_r(123);die;
        return $content
            ->header('审核列表')
            ->description()
            ->body($this->auditgrid());
    }
    public function auditgrid(){
        return new Grid(Media::where(['status'=>0,'is_user'=>1]), function (Grid $grid) {
            // $grid->disableColumnSelector();
            $grid->column('id','ID');
            $grid->column('title','标题');
            $grid->column('remark','备注');
            $grid->column('src','路径')->image();
            $grid->column('created_at','创建时间');
            $grid->column('status','审核结果')->select(['待审核','审核通过', '审核失败'],true);
            // 启用边框模式
            $grid->withBorder();
            $grid->fixColumns(2);
            //显示详情按钮
            // $grid->showViewButton();
            $grid->disableViewButton();
            //操作按钮
            // $grid->showActions();
            $grid->disableActions();
            // $grid->disableBatchDelete();
            // 创建按钮
            $grid->showCreateButton();
            // 禁用创建按钮
            // $grid->disableCreateButton();
            // 禁用详情按钮
            // $grid->disableViewButton();
            // 禁用编辑按钮
            // $grid->disableEditButton();
            // 显示编辑按钮
            $grid->showEditButton();
            // 禁用快捷编辑按钮
            $grid->disableQuickEditButton();
            // 显示快捷编辑按钮
            // $grid->showQuickEditButton();
            // 设置弹窗宽高，默认值为 '700px', '670px'
            // $grid->setDialogFormDimensions('50%', '60%');
            // $grid->enableDialogCreate();
            // 禁用删除按钮
            $grid->disableDeleteButton();
            // 显示删除按钮
            // $grid->showDeleteButton();
        });
    }
    protected function grid()
    {
        // if(Admin::user()->id==1){
            // $where = array();
        // }else{
        //     $user_id = isset(Admin::user()->pid)?Admin::user()->pid:Admin::user()->id;
        //     $where['user_id'] = $user_id;
        // }
        // if($id!=0){
        //     $where['companyId'] = $id;
        // }

        return new Grid(Media::where(['user_id'=>Admin::user()->id]), function (Grid $grid) {
            $grid->disableColumnSelector();
            $grid->column('id','ID');
            $grid->column('title','标题');
            $grid->column('remark','备注');
            $grid->column('src','内容')->image();
            $grid->column('created_at','创建时间');
            $grid->column('status',trans('审核状态'))->using(['待审核','审核通过', '审核失败']);
            // $grid->column('status','审核结果')->select(['待审核','审核通过', '审核失败'],true);
            // 启用边框模式
            $grid->withBorder();
            $grid->fixColumns(2);
            //显示详情按钮
            // $grid->showViewButton();
            $grid->disableViewButton();
            //操作按钮
            $grid->showActions();
            // $grid->disableActions();
            // $grid->disableBatchDelete();
            // 创建按钮
            $grid->showCreateButton();
            // 禁用创建按钮
            // $grid->disableCreateButton();
            // 禁用详情按钮
            // $grid->disableViewButton();
            // 禁用编辑按钮
            // $grid->disableEditButton();
            // 显示编辑按钮
            $grid->showEditButton();
            // 禁用快捷编辑按钮
            // $grid->disableQuickEditButton();
            // 显示快捷编辑按钮
            $grid->showQuickEditButton();
            // 设置弹窗宽高，默认值为 '700px', '670px'
            // $grid->setDialogFormDimensions('50%', '60%');
            // $grid->enableDialogCreate();
            // 禁用删除按钮
            $grid->disableDeleteButton();
            // 显示删除按钮
            // $grid->showDeleteButton();
        });
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新增媒体')
            ->description('创建')
            // ->body($this->buildPreviewButton())
            // ->body($this->newline())
            ->body($this->form());
    }
    public function form(){
        return Form::make(Media::where(['user_id'=>Admin::user()->id]), function (Form $form) {
            $form->block(8, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('基本设置');
                // 显示底部提交按钮
                $form->showFooter();
                // 设置字段宽度
                $form->width(9, 3);
                $form->column(6, function (Form\BlockForm $form) {
                    $form->text('title','媒体名称')->required();
                    $u = Admin::user()->id==1?0:1; //0平台 1用户自传
                    $form->hidden('is_user')->value($u);
                    $form->hidden('user_id')->value(Admin::user()->id);
                    $form->text('remark','媒体说明');
                    $form->select('type', '媒体类型')->options([
                        0 => '图片',
                        1 => '视频',
                    ]);
                    $form->file('src','平面图')->removable(false)->autoUpload();
                    $form->hidden('status')->customFormat(function ($v) {
                            return $v == '打开' ? 1 : 0;
                        })->saving(function ($v) {
                            return $v ? 1 : 0;
                        });
                });
            });
            // $form->saving(function (Form $form){

            // });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('媒体编辑')
            ->description('编辑')
            // ->body($this->buildPreviewButton())
            // ->body($this->newline())
            ->body($this->form()->edit($id));
    }

    public function show($id){
        return Show::make($id, new Media(), function (Show $show) {
            // $show->layerId;
            $show->field('title','项目名称');
            // $show->field('remark','公司备注');
            $show->created_at;
        });
    }
    //审核提交
    public function postAudit(Request $request){
        $info = $request->all();
        $media = Media::where(['id'=>$info['id'],'status'=>0])->first()->toArray();
        if ($media) {
            $param['admin_id'] = $info['aid'];
            if(isset($info['reason'])){
                $param['status'] = 2;
                $param['reason'] = $info['reason'];
            }else{
                $param['status'] = 1;
            }
            Media::where(['id'=>$info['id']])->update($param);
            return array('msg'=>'审核成功！','status'=>1);
        }else{
            return array('msg'=>'审核失败！','status'=>0);
        }
    }
}